var _excluded = ['value', 'onChange', 'options', 'className'];
function _extends() {
  _extends = Object.assign
    ? Object.assign.bind()
    : function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;
      };
  return _extends.apply(this, arguments);
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
import { Select as AntdSelect, Input } from 'antd';
import 'antd/es/select/style/index.js';
import classnames from 'classnames';
import React, { useCallback, useRef } from 'react';
import { useFieldContext } from '../../_utils/contexts/FieldContext';
import useOther from '../../_utils/hooks/useOther';
import useRequestOptions from '../../_utils/hooks/useRequestOptions';
import createField from '../../createField';
import './index.less';
/**
 * 包含其他选项的下拉框
 */
var SelectWithOther = function SelectWithOther(_ref) {
  var value = _ref.value,
    onChange = _ref.onChange,
    optionsOrRequest = _ref.options,
    className = _ref.className,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useFieldContext = useFieldContext(),
    label = _useFieldContext.label;
  var _useRequestOptions = useRequestOptions(optionsOrRequest),
    options = _useRequestOptions.options;
  var otherRef = useRef('select-other-' + Math.random());
  var _useOther = useOther({
      mode: 'single',
      value: value,
      onChange: onChange,
      options: options,
    }),
    otherChecked = _useOther.otherChecked,
    changeOtherChecked = _useOther.changeOtherChecked,
    selectedValue = _useOther.selectedValue,
    changeSelectedValue = _useOther.changeSelectedValue,
    otherValue = _useOther.otherValue,
    changeOtherValue = _useOther.changeOtherValue;
  var handleSelectChange = useCallback(
    function (val) {
      var found = options.find(function (option) {
        return option.value === val;
      });
      if (found) {
        changeOtherChecked(false);
        changeSelectedValue(found.value);
      } else {
        changeOtherChecked(true);
      }
    },
    [options, changeSelectedValue, changeOtherChecked],
  );
  var handleInputChange = useCallback(
    function (event) {
      changeOtherValue(event.target.value);
    },
    [changeOtherValue],
  );
  return /*#__PURE__*/ React.createElement(
    'span',
    {
      className: classnames('szzj-select-with-other', className),
    },
    /*#__PURE__*/ React.createElement(
      AntdSelect,
      // @fixme 值可能为 boolean，不满足类型校验
      // @ts-ignore
      _extends(
        {
          value: otherChecked
            ? otherRef.current
            : selectedValue === null || selectedValue === void 0
            ? void 0
            : selectedValue[0],
          onChange: handleSelectChange,
          placeholder: '\u8BF7\u9009\u62E9'.concat(label),
        },
        rest,
        {
          className: 'szzj-select-with-other-select',
        },
      ),
      (options || []).map(function (option) {
        return /*#__PURE__*/ React.createElement(
          AntdSelect.Option,
          {
            key: option.value,
            value: option.value,
          },
          option.label,
        );
      }),
      /*#__PURE__*/ React.createElement(
        AntdSelect.Option,
        {
          value: otherRef.current,
        },
        '\u5176\u4ED6',
      ),
    ),
    otherChecked
      ? /*#__PURE__*/ React.createElement(Input, {
          value: otherChecked ? otherValue : undefined,
          onChange: handleInputChange,
          maxLength: 100,
          className: 'szzj-select-with-other-input',
        })
      : null,
  );
};
export default createField(SelectWithOther);
